<?php

namespace app\admin\model\gifts;

use think\Model;

/**
 * Template
 */
class Template extends Model
{
    // 表名
    protected $name = 'gifts_template';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 字段类型转换
    protected $type = [
        'delete_time' => 'timestamp:Y-m-d H:i:s',
    ];

    protected static function onAfterInsert($model): void
    {
        if ($model->weigh == 0) {
            $pk = $model->getPk();
            if (strlen($model[$pk]) >= 19) {
                $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
            } else {
                $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
            }
        }
    }

    public function getImageAttr($value): string
    {
        return get_oss_sign_url($value);
    }
}